package com.fastloan.mgt.overwrite;

import com.fastloan.mgt.customer.enums.CustomerEnums;
import com.fastloan.mgt.customer.service.impl.ZyCustomerCardServiceImpl;
import com.fastloan.mgt.daily.service.WCustomerManagerDailyService;
import com.fastloan.mgt.expert.model.Expert;
import com.fastloan.mgt.expert.service.ExpertService;
import com.fastloan.mgt.manager.model.Manager;
import com.fastloan.mgt.manager.service.ManagerService;
import com.fastloan.safe.core.param.RoleTypeEnum;
import com.fastloan.safe.mgt.auth.model.CmuRole;
import com.fastloan.safe.mgt.auth.service.CmuRoleService;
import com.fastloan.safe.mgt.org.model.CmuOrg;
import com.fastloan.safe.mgt.org.service.CmuOrgService;
import com.fastloan.safe.mgt.user.model.CmuUser;
import com.fastloan.safe.mgt.user.service.impl.CmuUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author rankai
 *         createTime 2017-12-2017/12/7 10:35
 */
@Service
public class ZyUserServiceImpl extends CmuUserServiceImpl {

    @Autowired
    private CmuOrgService orgService;

    @Autowired
    private ManagerService managerService;

    @Autowired
    private ExpertService expertService;

    @Autowired
    private CmuRoleService roleService;

    @Autowired
    private WCustomerManagerDailyService managerDailyService;

    @Override
    public void insertWithRole(CmuUser user, String[] roleIds, String orgId, boolean isAdmin, MultipartFile... files) {
        CmuOrg cmuOrg = orgService.selectByPrimaryKey(orgId);
        if (cmuOrg.getOrgType() == 1) {
            user.setVerify(CustomerEnums.VerifyEnum.NO_VERIFY.getCode());
        } else {
            user.setVerify(CustomerEnums.VerifyEnum.IS_VERIFY.getCode());
        }
        super.insertWithRole(user, roleIds, orgId, isAdmin, files);
        addUserAfter(user.getId(), roleIds);
    }

    @Override
    public void updateWithRole(CmuUser user, String[] roleIds, MultipartFile logoFile, boolean isAdmin) {
        super.updateWithRole(user, roleIds, logoFile, isAdmin);
    }

    private void addUserAfter(String userId, String[] roleIds) {
        for (String roleId : roleIds) {
            CmuRole cmuRole = roleService.selectByPrimaryKey(roleId);
            if (RoleTypeEnum.MANAGER.getCode() == cmuRole.getIndexType()) {
                Manager manager = new Manager();
                manager.setUserId(userId);
                managerService.insertSelective(manager);
                managerDailyService.addCustomerManagerDailyCommit(userId);
            } else if (RoleTypeEnum.EXPERT.getCode() == cmuRole.getIndexType()) {
                Expert expert = new Expert();
                expert.setUserId(userId);
                expertService.insertSelective(expert);
            }
        }
    }
}
